/*
 * Copyright (C) 2017 Honeywell, Inc. All Rights Reserved.
 */

package org.thanos.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.thanos.demo.topic.DemoTopic;

import java.util.UUID;
import java.util.concurrent.CompletableFuture;


@Component
public class RedisAppListener implements ApplicationListener<ApplicationReadyEvent> {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) {
        CompletableFuture.runAsync(() -> {
            long time = System.currentTimeMillis();
            for (; ; ) {
                if (System.currentTimeMillis() - time > 1000) {
                    redisTemplate.convertAndSend(DemoTopic.CHANNEL.getTopic(), UUID.randomUUID().toString());
                    time = System.currentTimeMillis();
                }
            }
        });
    }
}
